package edu.syr.iis.web.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import edu.syr.iis.web.model.User;

public class HttpRequestInterceptor extends HandlerInterceptorAdapter {
	
	public String signInPage;
	
	private SecurityManager securityManager;
	
	@Override
	public boolean preHandle(HttpServletRequest request, 
							 HttpServletResponse response,
							 Object handler)
	throws Exception
	{		
		if (request.getRequestURI().equals(request.getContextPath() + "/" + signInPage))
			return true;
		
		User user = securityManager.getUser(request);
		
		if (user == null) 
		{
			request.getSession().setAttribute("forward_to",request.getServletPath());
			response.sendRedirect(signInPage);
			return false;
		}
		else
		{
			securityManager.updateAnnotationSession(request);
		}
		
		return true;
	}

	public String getSignInPage() {
		return signInPage;
	}

	public void setSignInPage(String signInPage) {
		this.signInPage = signInPage;
	}

	public SecurityManager getSecurityManager() {
		return securityManager;
	}

	public void setSecurityManager(
			SecurityManager securityManager) {
		this.securityManager = securityManager;
	}
	

}
